﻿//********************************************************************************//
//创建日期：<创建日期，2016-04-25>
//创建作者：<朱大鹏，zhudapeng@shanghai3h.com>
//功能说明：来函登记管理
//********************************************************************************//

using System;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Web.Class;
using DevExpress.Web;


namespace Web.Pages.YongShui._13.LAIHANGL
{
    public partial class LAIHANDJGL : BasePage
    {
        #region 属性
        /// <summary>
        /// 保存gv数据
        /// </summary>
        public DataTable DataViewSource
        {
            get { return ViewState["DataViewSource"] != null ? ViewState["DataViewSource"] as DataTable : null; }
            set { ViewState["DataViewSource"] = value; }
        }
        #endregion

        #region 变量
        /// <summary>
        /// 数据库访问
        /// </summary>
        SpHelper _sphelper = new SpHelper();
        /// <summary>
        /// 访问管理块信息
        /// </summary>
        YS_JX_GUANLIKBLL glkbll = new YS_JX_GUANLIKBLL();

        AS_UserInfoBLL BllUser = new AS_UserInfoBLL();
        #endregion

        #region 事件
        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
                FouseChange();
            }
            LoadData();
        }
        /// <summary>
        /// 树形菜单改变选项时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void treeGuanLiK_FocusedNodeChanged(object sender, EventArgs e)
        {
            FouseChange();
        }
        /// <summary>
        /// 隐藏按钮事件   当文本框中按回车时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSearch_Click(object sender, EventArgs e)
        {
            FouseChange();
        }
        /// <summary>
        /// 下拉框选项改变时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DrpLX_SelectIndexChange(object sender, EventArgs e)
        {
            FouseChange();
        }
        /// <summary>
        /// 修改按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnEdit_Click(object sender, EventArgs e)
        {
            EditData();
        }
        protected void BtnBindGV_Click(object sender, EventArgs e)
        {
            FouseChange();
        }
        #endregion
        
        #region 方法
        /// <summary>
        /// 获得当前选中行
        /// </summary>
        private void EditData()
        {
            int index = gvWenJianGZ.FocusedRowIndex;
            if (index > -1)
            {
                object tempColumnValues = this.gvWenJianGZ.GetRowValues(index, "S_LAIHANBH");
                object ID = this.gvWenJianGZ.GetRowValues(index, "ID");
               System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "edit" + DateTime.Now.ToString("yyyymmddhhmmssfff"), new System.Text.StringBuilder("OpenCuoWuXX('").Append(tempColumnValues).Append("',1,"+ID+");").ToString(), true);
       
               
            }
            else
            {
                PageTools.AlertMsg(this, "请选择数据后继续操作");
            }

        }
        /// <summary>
        /// 条件改变时根据条件绑定数据
        /// </summary>
        private void FouseChange()
        {
            if (null != treeGuanLiK.FocusedNode)
            {
                var node = treeGuanLiK.FocusedNode;
                if (node.Level == 2)
                {
                    string GuanLiKuaiID = ((System.Data.DataRowView)(node.DataItem)).Row.ItemArray[1].ToString().Split('_')[0];
                    string year = ((System.Data.DataRowView)(node.DataItem)).Row.ItemArray[1].ToString().Split('_')[1];
                    
                    BindGV(GuanLiKuaiID, year);
                }
                else
                {
                    if (node.ChildNodes.Count > 0)
                    {
                        var childNode = node.ChildNodes[0];
                        string GuanLiKuaiID = ((System.Data.DataRowView)(childNode.DataItem)).Row.ItemArray[1].ToString().Split('_')[0];
                        string year = ((System.Data.DataRowView)(childNode.DataItem)).Row.ItemArray[1].ToString().Split('_')[1];
                        BindGV(GuanLiKuaiID, year);

                    }
                    else
                    {
                        string GuanLiKuaiID = ((System.Data.DataRowView)(node.DataItem)).Row.ItemArray[1].ToString().Split('_')[0];
                        BindGV(GuanLiKuaiID, "0");
                    }
                }
            }
        }
        /// <summary>
        /// 绑定gv数据
        /// </summary>
        /// <param name="glk"></param>
        /// <param name="year"></param>
        private void BindGV(string glk,string year)
        {
            DataTable dtZhandian = BusinessTool.GetTextAndValue("view_as_userinfo", "ZHANDIANMC", "ZHANDIANBH", null, string.Format("account='{0}'", LoginInfo.CurrentUser.Account)).Tables[0];
            string zhandianBH = "";
            if (dtZhandian != null && dtZhandian.Rows.Count > 0)
            {
                zhandianBH = dtZhandian.Rows[0]["VALUE"].ToString();
            }
            DataSet ds = _sphelper.P_YS_LH_LAIHANXXCX(glk, year, txtLHBH.Text.Trim(), txtYHH.Text.Trim(), txtXKH.Text.Trim(), Convert.ToInt32(DrpLX.Value), zhandianBH == "00" ? null : zhandianBH);
            DataViewSource = ds.Tables[0];
            gvWenJianGZ.DataSource = DataViewSource;
            gvWenJianGZ.DataBind();

           
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                //lanhanbh.Value = ds.Tables[0].Rows[0]["ID"].ToString();
                gvWenJianGZ.FocusedRowIndex = -1;
                gvWenJianGZ.FocusedRowIndex = 0;
            }
        }
        public string GetYeWuYuanXM(string id)
        {
            if (id.Trim().Length == 0)
            {
                return "";
            }
            string name = "";
            DataTable user = BusinessTool.GetTextAndValue("view_as_userinfo", "UserName", "USERID", null, string.Format("USERID={0}", id)).Tables[0];
            if (user != null && user.Rows.Count > 0)
            {
                name = user.Rows[0]["Text"].ToString();
            }
            return name;
        }
        /// <summary>
        /// 初始化页面绑定数据
        /// </summary>
        private void InitControl()
        {


            DataSet ds = _sphelper.P_YS_LH_LAIHANLX(BusinessTool.WType);

            DrpLX.DataSource = ds;
            DrpLX.TextField = "S_LAIHANLXMC";
            DrpLX.ValueField = "I_LAIHANH";
            DrpLX.DataBind();

            DrpLX.Items.Insert(0,new ListEditItem("全部","0"));

            DrpLX.SelectedIndex = 0;

            LoadData();
        }
        /// <summary>
        /// 绑定树形
        /// </summary>
        private void LoadData()
        {
            DataTable dtZhandian = BusinessTool.GetTextAndValue("view_as_userinfo", "ZHANDIANMC", "ZHANDIANBH", null, string.Format("account='{0}'", LoginInfo.CurrentUser.Account)).Tables[0];
            string zhandianBH = "";
            if (dtZhandian != null && dtZhandian.Rows.Count > 0)
            {
                zhandianBH = dtZhandian.Rows[0]["VALUE"].ToString();
            }
            if (zhandianBH == "")
            {
                return;
            }

            DataSet glkdt = _sphelper.P_YS_LH_GUANLIKYM(zhandianBH == "00" ? null : zhandianBH,2);
            if (null != glkdt && glkdt.Tables.Count > 0)
            {
                var table = glkdt.Tables[0];
                //DataRow dr = glkdt.Tables[0].NewRow();
                //dr[0] = "全部";
                //dr[1] = 0;
                //dr[2] = 0;
                //glkdt.Tables[0].Rows.InsertAt(dr, 0);
                treeGuanLiK.KeyFieldName = "S_GUANLIKH";
                treeGuanLiK.ParentFieldName = "ParentId";
                treeGuanLiK.DataSource = table;
                treeGuanLiK.DataBind();
            }
            gvWenJianGZ.DataSource = DataViewSource;
            gvWenJianGZ.DataBind();
        }
        #endregion

        
    }
}